package cn.wolfcode.wolf2w.advice;

import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CommonExceptionHandler {

    /**
     * @ControllerAdvice controller 功能增强注解
     *
     * 请求加入映射方法前,功能增强:统一异常处理
     *请求加入映射方法前,功能增强:date类型参数格式化
     *
     * 处理异常方法定义和请求映射方法一样
     *
     *
     * @param
     * @return
     */

    //自定义异常
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object handlerLogic(Exception e){
        e.printStackTrace();
        return new JsonResult<>(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
    }

    //系统异常
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object handlerExt(Exception e){
        e.printStackTrace();
        return JsonResult.defaultError();
    }
}
